package com.ly.lw.styledo.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.Window;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.ly.lw.styledo.R;
import com.ly.lw.styledo.fragment.BrandFragment;
import com.ly.lw.styledo.fragment.CategoryFragmentHome;
import com.ly.lw.styledo.fragment.MainFragment;
import com.ly.lw.styledo.fragment.MyFragment;
import com.ly.lw.styledo.utils.FragmentTableUtils;
import com.ly.lw.styledo.utils.LocationHelper;
import com.umeng.message.PushAgent;
import com.umeng.message.UmengRegistrar;

import java.util.ArrayList;


public class MainActivity extends FragmentActivity implements LocationHelper.LocationLinstener{


    @ViewInject(R.id.textId)
    private TextView textView;

    Boolean isClick = false;


    @ViewInject(R.id.rgs)
    private RadioGroup rgs;
    ArrayList<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        //-----友盟推送start--------
        PushAgent mPushAgent = PushAgent.getInstance(this);//开启推送服务
        mPushAgent.enable();
        PushAgent.getInstance(this).onAppStart();//统计应用启动数据
        String device_token = UmengRegistrar.getRegistrationId(this);//获取设备的Device Token
//        Log.i("tag", "--device_token--" + device_token);
        //-----友盟推送end--------

        ViewUtils.inject(this);
        fragments.add(new MainFragment());
        fragments.add(new CategoryFragmentHome());
        fragments.add(new BrandFragment());
        fragments.add(new MyFragment());
        //测试测试 
        new FragmentTableUtils(getSupportFragmentManager(), fragments, R.id.main_fragment, rgs);

    }


    public void onClick(View view){
        //Toast.makeText(getApplicationContext(),"hehe",Toast.LENGTH_SHORT).show();

        LocationHelper.getInstance(this).setLocationLinstener(this);
        LocationHelper.getInstance(this).start();


    }

    private long firstTime;

    @Override
    public void onBackPressed() {
        long secondTime = System.currentTimeMillis();
        if (secondTime - firstTime > 2000) {
            Toast.makeText(this, "再点击一次将退出应用程序", Toast.LENGTH_SHORT).show();
            firstTime = secondTime;//更新firstTime
            return;
        } else {
            //两次按键小于2秒时，退出应用
            android.os.Process.killProcess(android.os.Process.myPid());
        }
    }


    /**
     * 接口方法一   返回经纬度
     * @param longitude
     * @param latitude
     */

    @Override
    public void locationLinstener(double longitude, double latitude) {

       // Toast.makeText(getApplicationContext(),"经度"+longitude,Toast.LENGTH_SHORT).show();
        //textView.setText("经度"+longitude);
        //Toast.makeText(getApplicationContext(),"经度"+longitude+"维度"+latitude,Toast.LENGTH_SHORT).show();
    }


    /**
     * 接口方法二   返回城市街道
     * @param province
     * @param city
     * @param street
     */
    @Override
    public void locationLinstenerSendCity(String province, String city, String street) {
        //Toast.makeText(getApplicationContext(),city+street,Toast.LENGTH_SHORT).show();
        textView.setText(city + street);
    }
}
